﻿using CommonServiceLocator;
using GalaSoft.MvvmLight.CommandWpf;
using HuiZe.education.Common;
using HuiZe.education.Model;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;

namespace HuiZe.education.ViewModel
{
    public class ResourceCenterViewModel : ViewModelBase
    {


        public int pageCount { get; set; }
        public int pageCount2 { get; set; }
        public ObservableCollection<ResourceCenterModel> Resources { get; set; }
        public List<respResourceDetailModel> Kind_items { get; set; }
        public List<respResourceChildModel> respResourceList { get; set; }
        public RelayCommand<object> ReturnToLessonHomeCommand
        {
            get
            {
                return new RelayCommand<object>(new Action<object>((o) =>
                {
                    MainViewModel mainViewModel = ServiceLocator.Current.GetInstance<MainViewModel>();
                    mainViewModel.NavChanged(o.ToString());
                }));
            }
        }

        public ResourceCenterViewModel()
        {
            //GetSourceList();
            //GetCategoryCourseList();
        }

        public void GetSourceList()
        {
            Resources = new ObservableCollection<ResourceCenterModel>();
            ReqresourceCenterModel model = new ReqresourceCenterModel();
            model.token = HuiZeeducation.Instance.UserInfo.token;
            string data = JsonHelper.ToJson(model);
            var result = Request<List<respResourceChildModel>>("/api/ziyuan/getKindList", data, "获取资源列表");
            if (result.Item1)
            {
                var resources = result.Item2.data;
                respResourceList = result.Item2.data;
                foreach (var res in resources)
                {
                    var resource = new ResourceCenterModel
                    {
                        kind_name = res.kind_name,
                        id = res.id
                    };
                    Resources.Add(resource);
                }
            }
            else
            {
                if (result.Item2 != null)
                    ShowDialogView(result.Item2.msg);
            }
        }

        public void GetCategoryItemList(string ContentType)
        {
            //foreach (var res in respResourceList)
            //{
            //    if (res.kind_name == ContentType)
            //    {
            //        Kind_items = res.kind_item;
            //    }
            //}
        }

        public List<resCategoryCourseChildModel> GetCategoryCourseList(int id, string type)
        {
            ReqresourceDetailModel model = new ReqresourceDetailModel()
            {
                token = HuiZeeducation.Instance.UserInfo.token,
                id = id,
            };

            string data = JsonHelper.ToJson(model);
            var result = Request<resCategoryCourseModel>("/api/ziyuan/getZiyuanList", data, "获取资源列表");
            if (result.Item1)
            {
                return result.Item2.data.data;
            }
            else
            {
                if (result.Item2 != null)
                    ShowDialogView(result.Item2.msg);
                return null;
            }
        }


        public List<respResourcekinditemModel> GetKindItemList(int Id, string type)
        {
            Resources = new ObservableCollection<ResourceCenterModel>();
            ReqresourceCenterModel2 model = new ReqresourceCenterModel2()
            {
                token = HuiZeeducation.Instance.UserInfo.token,
                id = Id,
                limit = 15,
                p = 1,
            };
            string data = JsonHelper.ToJson(model);
            var result = Request<respResourcekinditemMainModel>("/api/ziyuan/getKind_itemList", data, "获取类别下的课程");
            if (result.Item1)
            {
                pageCount = result.Item2.data.last_page;
                //treenodes.Clear();
                return result.Item2.data.data;
            }
            else
            {
                if (result.Item2 != null)
                    ShowDialogView(result.Item2.msg);
                return null;
            }
        }

        public List<respResourcekinditemModel> GetKindItemList(int _id, int _page_index)
        {
            Resources = new ObservableCollection<ResourceCenterModel>();
            ReqresourceCenterModel2 model = new ReqresourceCenterModel2()
            {
                token = HuiZeeducation.Instance.UserInfo.token,
                id = _id,
                limit = 15,
                p = _page_index,
            };
            string data = JsonHelper.ToJson(model);
            var result = Request<respResourcekinditemMainModel>("/api/ziyuan/getKind_itemList", data, "获取类别下的课程");
            if (result.Item1)
            {
                pageCount = result.Item2.data.last_page;
                return result.Item2.data.data;
            }
            else
            {
                if (result.Item2 != null)
                    ShowDialogView(result.Item2.msg);
                return null;
            }
        }

        public respResourceChildModel GetCategoryCourseList(int id)
        {
            ReqresourceDetailModel model = new ReqresourceDetailModel()
            {
                token = HuiZeeducation.Instance.UserInfo.token,
                id = id,
                limit = 200,
            };

            string data = JsonHelper.ToJson(model);
            var result = Request<respResourceChildModel>("/api/ziyuan/getZiyuanList", data, "获取资源列表");
            if (result.Item1)
            {
                return result.Item2.data;
                //return result.Item2.data.data;
            }
            else
            {
                if (result.Item2 != null)
                    ShowDialogView(result.Item2.msg);
                return null;
            }
        }

        public List<reqsFileType> GetFileType()
        {
            ReqresourceDetailModel model = new ReqresourceDetailModel()
            {
                token = HuiZeeducation.Instance.UserInfo.token,

            };

            string data = JsonHelper.ToJson(model);
            var result = Request<List<reqsFileType>>("/api/ziyuan/getFileType", data, "获取资源列表");
            if (result.Item1)
            {
                return result.Item2.data;
            }
            else
            {
                if (result.Item2 != null)
                    ShowDialogView(result.Item2.msg);
                return null;
            }
        }

        public List<resMyResourceChildMainModel2> GetMySourceListList(int pages)
        {
            ReqreMysourceModel2 model = new ReqreMysourceModel2()
            {
                token = HuiZeeducation.Instance.UserInfo.token,
                p = pages,
                limit = 15,
            };
            string data = JsonHelper.ToJson(model);
            var result = Request<resMyResourceModel2>("/api/ziyuan/getMyZiyuanList", data, "获取资源列表");
            if (result.Item1)
            {
                pageCount2 = result.Item2.data.last_page;
                return result.Item2.data.data;
            }
            else
            {
                if (result.Item2 != null)
                    ShowDialogView(result.Item2.msg);
                return null;
            }



        }

        public void AddCollection(int id)
        {
            ReqAddCollectionModel model = new ReqAddCollectionModel()
            {
                token = HuiZeeducation.Instance.UserInfo.token,
                id = id,
            };
            string data = JsonHelper.ToJson(model);
            var result = Request<resAddCollectionMainModel>("/api/ziyuan/addCollection", data, "添加资源");
            if (result.Item1)
            {
                ShowDialogView(result.Item2.msg);
            }
            else
            {
                if (result.Item2 != null)
                    ShowDialogView(result.Item2.msg);
            }
        }

        public bool Collect(int id, bool _show_msg = true)
        {
            ReqAddCollectionModel model = new ReqAddCollectionModel()
            {
                token = HuiZeeducation.Instance.UserInfo.token,
                id = id,
            };
            string data = JsonHelper.ToJson(model);
            var result = Request<resAddCollectionMainModel>("/api/ziyuan/addCollection", data, "添加资源");
            if (result.Item1)
            {
                if (_show_msg)
                    ShowDialogView(result.Item2.msg);
                return true;
            }
            else
            {
                if (result.Item2 != null)
                    ShowDialogView(result.Item2.msg);
            }
            return false;
        }
    }
}
